<?php
/***************************************************************************
 *   copyright : (C) 2009 Udrea Cristian
 *   site : http://code.google.com/p/testauction-php
 ***************************************************************************/

/***************************************************************************
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as 
 *   published by the Free Software Foundation; either version 2 of the
 *   License, or (at your option) any later version. Although none of the
 *   code may be sold. If you have been sold this script, get a refund.
 ***************************************************************************/
include(APP_ROOT.'include/classes/constants.php');
include('../library/functions.php');
#=======CONSTANTS=======
$ROWSPERPAGE = 10;
#=======================

if (isset($_GET['status']) && $_GET['status'] != '') {
	$status = $_GET['status'];
	$sql2   = " AND od_status = '$status'";
	$queryString = "&status=$status";
} else {
	$status = '';
	$sql2   = '';
	$queryString = '';
}	

// for paging
// how many rows to show per page
$ROWSPERPAGE = 10;

$sql = "SELECT o.od_id, o.auction_id, o.od_date, o.od_last_update, 	o.od_claim_type,"
	."o.od_claimed, o.od_status, o.od_shipping_userkey, p.product_name, p.product_id, u.username"
	." FROM ". TBL_ORDERS ." o, ". TBL_PRODUCTS ." p, ". TBL_AUCTIONS ." a, ". TBL_USERS ." u"
	." WHERE a.auction_id = o.auction_id AND a.product_id = p.product_id AND o.od_shipping_userkey = u.userkey $sql2"
	."GROUP BY od_id"
	." ORDER BY od_id DESC";

$result     = execute_query(getPagingQuery($sql, $ROWSPERPAGE));
$pagingLink = getPagingLink($sql, $ROWSPERPAGE, $queryString);

$orderStatus = array('New', 'Paid', 'Shipped', 'Completed', 'Cancelled');
$orderOption = '';
foreach ($orderStatus as $stat) {
	$orderOption .= "<option value=\"$stat\"";
	if ($stat == $status) {
		$orderOption .= " selected";
	}
	
	$orderOption .= ">$stat</option>\r\n";
}
?> 
<p>&nbsp;</p>
<form action="processOrder.php" method="post"  name="frmOrderList" id="frmOrderList">
 <table width="100%" border="0" cellspacing="0" cellpadding="2" class="text">
 <tr align="center"> 
  <td align="right">View</td>
  <td width="75"><select name="cboOrderStatus" class="box" id="cboOrderStatus" onChange="viewOrder();">
    <option value="" selected>All</option>
    <?php echo $orderOption; ?>
  </select></td>
  </tr>
</table>

 <table width="100%" border="0" align="center" cellpadding="2" cellspacing="1" class="text">
  <tr align="center" class="listTableHeader"> 
   <td width="60">No. Crt.</td>
   <td>User</td>
   <td width="60">Produs</td>
   <td width="150">Ultima Actualizare</td>
   <td width="70">Status</td>
  </tr>
  <?php
$parentId = 0;
if (count($result) > 0) {
	$i = 0;
	
	foreach($result as $row) {
		extract($row);
		
		if ($i%2) {
			$class = 'row1';
		} else {
			$class = 'row2';
		}
		
		$i += 1;
?>
  <tr class="<?php echo $class; ?>"> 
   <td width="60"><a href="<?php echo $_SERVER['PHP_SELF']; ?>?view=detail&oid=<?php echo $od_id; ?>"><?php echo $od_id; ?></a></td>
   <td><?php echo $username; ?></td>
   <td width="60" align="right"><a href="../product/index.php?view=detail&productId=<?php echo $product_id; ?>"><?php echo $product_name; ?></a></td>
   <td width="150" align="center"><?php echo $od_last_update; ?></td>
   <td width="70" align="center"><?php echo $od_status; ?></td>
  </tr>
  <?php
	} // end while
?>
  <tr> 
   <td colspan="5" align="center">
   <?php 
   echo $pagingLink;
   ?></td>
  </tr>
<?php
} else {
?>
  <tr> 
   <td colspan="5" align="center">No Orders Found </td>
  </tr>
  <?php
}
?>
 </table>
 <p>&nbsp;</p>
</form>